import Vue from 'vue'
import Vuex from 'vuex'
import modules from './modules'

import { uploadFile } from '@/api/upload'

// state
const state = {
    token: '', // token
    currentUser: {}, // 当前登陆的用户的信息
    autoLogin: 0, // 自动登陆 0表示关闭 1表示开启
    currentMode: 'web', // 当前的开发环境
    loginInfo: {}, // 登陆信息，自动登陆用到
    taskTimeDialogCircle: false, // 显示朋友圈定时任务
    currentNavs: {
        friendCircle: 'circleSend',
        groupAssistant: 'groupSend',
        tools: 'newFriend'
    }, // 每个页面选中的nav
    currentWechats: {
        circleSend: '',
        circleMy: '',
        circleLog: '',
        groupSend: '',
        groupLog: '',
        newFriend: '',
        addFriend: '',
        addFriendAuto: '',
        searchPhones: '',
        searchNumber: '',
        autoHi: '',
        qrCode: '',
        autoTask: '',
        autoReply: '',
        clearFans: '',
        phoneManagement: '',
    }, // 每个页面选中的微信
}

// getters
const getters = {
    token: state => state.token,
    currentUser: state => state.currentUser,
    userId: state => state.currentUser.SupplierId * 10000 + state.currentUser.UnionId || '',
    autoLogin: state => state.autoLogin,
    loginInfo: state => state.loginInfo,
    taskTimeDialogCircle: state => state.taskTimeDialogCircle,
    currentNavs: state => state.currentNavs,
    currentWechats: state => state.currentWechats,
    currentMode: state => state.currentMode,
}

// actions
const actions = {
    // 文件上传
    UploadFile({ commit }, formData) {
        return new Promise((resolve, reject) => {
            uploadFile(formData)
                .then(response => {
                    let data = response.data.data
                    resolve(data)
                })
                .catch(error => {
                    reject(error)
                })
        })
    }
}

// mutations
const mutations = {
    // 设置token
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    // 设置用户信息
    SET_USER: (state, userInfo) => {
        state.currentUser = userInfo
    },
    // 设置自动登陆
    SET_AUTO_LOGIN: (state, autoLogin) => {
        state.autoLogin = autoLogin
    },
    // 设置登陆的信息
    SET_LOGIN_INFO: (state, loginInfo) => {
        state.loginInfo = loginInfo
    },
    // 设置是否显示朋友圈定时任务日志
    SET_TASK_TIME_DIALOG_CIRLCE: (state, flag) => {
        state.taskTimeDialogCircle = flag
    },
    // 设置每个topnav当前的nav
    SET_CURRENT_NAV: (state, nav) => {
        Vue.set(state.currentNavs, state.route.name, nav.navPath)
    },
    SET_CURRENT_WECHATS: (state, wechatId) => {
        Vue.set(state.currentWechats, state.currentNavs[state.route.name], wechatId)
    },
    SET_CURRENT_MODE: (state, mode) => {
        state.currentMode = mode
    },
}

Vue.use(Vuex)

export default new Vuex.Store({
    modules,
    state,
    getters,
    mutations,
    actions,
    strict: process.env.NODE_ENV !== 'production'
})
